<?php 
$smarty = m::factory('smarty');
if (!$smarty->isCached('index.tpl','sidebar')){

$doc = Model::factory('article')->doc();
$doc_li = '';
foreach ($doc as $k=>$v){
	$doc_li .= '<li>';
	$doc_li .= '<span style="float:left">('.count($v).')</span>';
	$doc_li .= '<a href="'.project::a_url($k,false).'" class="row">';
	$doc_li .= $k.'</a></li>';
}
//var_dump($doc);

$read = Model::factory('article')->read_top();
$read_li = '';
foreach ($read as $_id=>$ips){
	$a = project::find_a($_id);
	$read_li .= '<li>';
	$read_li .= '<span style="float:left">('.count($ips).')</span>';
	$read_li .= '<a href="'.project::a_url($a).'" class="row" style="padding-left:3px;">';
	$read_li .= $a['title'].'</a></li>';
}
//var_dump($read);

$comment = MON::factory('comment')->find()->limit(5)->sort(array('addTime'=>-1));
$comment_li = '';
foreach ($comment as $v){
    if (!isset($v['article_id']) || !strlen($v['article_id'])) continue;
	$a = project::find_a($v['article_id']);
	$comment_li .= '<li>';
	$comment_li .= '<a href="'.project::a_url($a).'" class="row">'.$a['title'].'</a>';
	$comment_li .= '<span style="padding-left:10px;">'.$v['name'].'&nbsp;:&nbsp;'.common::cutstr(strip_tags(htmlspecialchars_decode($v['content'])),40).'</span></li>';
}
//var_dump($comment);

$tags_v = Model::factory('tag')->get_tags_anchor(null,'',array('class'=>'tag'));

$v = <<<EOT
<div id="sidebar">
<ul>
	<li>
	<h3><a class="search" style="text-decoration:none;">文章搜索</a></h3>
	<ul id="search">
		<li>&nbsp;</li>
		<li><form action="/search" method="get" style="padding:0px">
		<input type="text" tabindex="1" id="search_k" name="search_k" value="回车搜索" onblur="if(this.value == '')this.value='回车搜索';" onfocus="if(this.value == '回车搜索')this.value='';" style="width:190px;*width:170px;height:20px;background:url('/i/form_red.gif') repeat-x scroll left top #FFFFFF" />
		</li>
		<li>&nbsp;</li>
	</ul>
	</li>
	
	<li>
	<h3><a class="invoices" style="text-decoration:none;">阅读排行</a></h3>
	<ul id="read">
		{$read_li}
	</ul>
	</li>
	
	<li>
	<h3><a class="folder_table" style="text-decoration:none;">文章归档</a></h3>
	<ul id="doc">
		{$doc_li}
	</ul>
	</li>
	
	<li>
	<h3><a class="user" style="text-decoration:none;">近期评论</a></h3>
	<ul id="comment">
		{$comment_li}
	</ul>
	</li>
	
	<li>
	<h3><a class="page_white_link" style="text-decoration:none;">标签</a></h3>
	<div id="tag" style="margin-left:8px;width:160px;padding-top:5px;">{$tags_v}</div>
	</li>
	
	<!--<li>
	<h3><a class="page_white_link" style="text-decoration:none;">友情链接</a></h3>
	<ul id="read">
	<li><a style="padding-left:3px;"></a></li>
	</ul>
	</li>-->
</ul>
</div>
<style>
#sidebar a.row{padding:0px;width:160px;*width:150px;white-space:nowrap;display:inline-block;overflow:hidden;*float:left;}
#sidebar a.tag,a.tag:visited{display:inline-block;padding:0 2px;white-space:nowrap;font-size:12px;}
</style>
<script>
$(function(){
	if ($('#doc li').length > 5){
		$('#doc li').slice(5).wrapAll('<span id="doc_rest" style="display:none"></span>');
		$('#doc').append('<div id="doc_list_toggle" style="cursor:pointer;text-align:center">▼</div>');
		$('#doc_list_toggle').toggle(function(){
			$('#doc_rest').show('slow');
			$(this).text('▲');
		},function(){
			$('#doc_rest').hide('slow');
			$(this).text('▼');
		});
	}

	if (cookie.get('search_k')){
		$('input[name="search_k"]').val(cookie.get('search_k'));
		cookie.del('search_k');
	}
	
	if ($('#tag').size() > 0){
		$('#tag a:lt(5)').css('font-size','20px');
		$('#tag a:gt(4):lt(10)').css('font-size','16px');
		$('#tag a:gt(9):lt(20)').css('font-size','14px');
		total = $('#tag a').size();
		for (i=0;i<total;i++){
			$('#tag a').last().insertBefore($('#tag a').eq(rand(total-1)-1));
		}
	}
})
</script>
EOT;

$smarty->assign('template',$v);

}
return $smarty->fetch('index.tpl','sidebar');